package com.cocolove2.library_cocodialog.dialogs;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.cocolove2.library_cocodialog.CornerUtils;
import com.cocolove2.library_cocodialog.dialogs.base.CocoListDialogBase;
import com.cocolove2.library_cocodialog.entity.DialogMenuItem;

/**
 * Dialog like iOS ActionSheet(iOS风格对话框)
 */
@SuppressWarnings("unused")
public class ActionSheetDialog extends CocoListDialogBase<ActionSheetDialog> {
    /* title underline(标题下划线)*/
    private View mVLineTitle;
    /*mCancel button(取消按钮)*/
    private TextView mTvCancel;
    /* title height(标题栏高度)*/
    private float mTitleHeight = 48;
    /* item height(ListView item高度) */
    private float mItemHeight = 48;
    /*cancel btn text(取消按钮内容)*/
    private String mCancelText = "取消";
    /*cancel btn text color(取消按钮文字颜色)*/
    private int mCancelTextColor = Color.parseColor("#44A2FF");
    /*cancel btn text size(取消按钮文字大小)*/
    private float mCancelTextSize = 17.5f;

    public ActionSheetDialog(Context context) {
        super(context);
        init();
    }

    public ActionSheetDialog(Context context, int style) {
        super(context, style);
        init();
    }


    private void init() {
        mTitleBgColor = Color.parseColor("#ddffffff");
        mTitleTextColor = Color.parseColor("#8F8F8F");
        mTitleTextSize = 17.5f;
        mDialogBgColor = Color.parseColor("#ddffffff");
        mDividerColor = Color.parseColor("#D7D7D9");
        mDividerHeight = 0.8f;
        mItemPressColor = Color.parseColor("#ffcccccc");
        /** item textcolor(ListView item文字颜色) */
        mItemTextColor = Color.parseColor("#44A2FF");

        /** LayoutAnimation */
        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
                0f, Animation.RELATIVE_TO_SELF, 6f, Animation.RELATIVE_TO_SELF, 0);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.setDuration(350);
        animation.setStartOffset(150);

        mLac = new LayoutAnimationController(animation, 0.12f);
        mLac.setInterpolator(new DecelerateInterpolator());
    }

    /**
     * set titleHeight(设置标题高度)
     */
    public ActionSheetDialog setTitleHeight(float titleHeight) {
        mTitleHeight = titleHeight;
        return this;
    }

    /**
     * set item height(item高度)
     */
    public ActionSheetDialog setItemHeight(float itemHeight_DP) {
        mItemHeight = itemHeight_DP;
        return this;
    }

    @Override
    protected View getAdapterView(int position, View convertView, ViewGroup parent) {
        final DialogMenuItem item = mContents.get(position);

        LinearLayout llItem = new LinearLayout(mContext);
        llItem.setOrientation(LinearLayout.HORIZONTAL);
        llItem.setGravity(Gravity.CENTER_VERTICAL);

        ImageView ivItem = new ImageView(mContext);
        ivItem.setPadding(0, 0, dp2px(15), 0);
        llItem.addView(ivItem);

        TextView tvItem = new TextView(mContext);
        tvItem.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        tvItem.setSingleLine(true);
        tvItem.setGravity(Gravity.CENTER);
        tvItem.setTextColor(mItemTextColor);
        tvItem.setTextSize(TypedValue.COMPLEX_UNIT_SP, mItemTextSize);
        tvItem.setHeight(dp2px(mItemHeight));

        llItem.addView(tvItem);
        float radius = dp2px(mCornerRadius);
        if (mTvTitle.getVisibility() == View.VISIBLE) {
            llItem.setBackground((CornerUtils.listItemSelector(radius, Color.TRANSPARENT, mItemPressColor,
                    position == mContents.size() - 1)));
        } else {
            llItem.setBackground(CornerUtils.listItemSelector(radius, Color.TRANSPARENT, mItemPressColor,
                    mContents.size(), position));
        }

        ivItem.setImageResource(item.mResId);
        tvItem.setText(item.mOperName);
        ivItem.setVisibility(item.mResId == 0 ? View.GONE : View.VISIBLE);

        return llItem;
    }

    /**
     * set cancel btn text(设置取消按钮内容)
     */
    public ActionSheetDialog setCancelText(String cancelText) {
        mCancelText = cancelText;
        return this;
    }

    /**
     * cancel btn text color(取消按钮文字颜色)
     */
    public ActionSheetDialog setCancelTextColor(int cancelTextColor) {
        mCancelTextColor = cancelTextColor;
        return this;
    }

    /**
     * cancel btn text size(取消按钮文字大小)
     */
    public ActionSheetDialog setCancelTextSize(float cancelTextSize) {
        mCancelTextSize = cancelTextSize;
        return this;
    }

    @Override
    protected View onCreateView(Context context, ViewGroup parent) {
        LinearLayout ll_container = new LinearLayout(mContext);
        ll_container.setOrientation(LinearLayout.VERTICAL);
        ll_container.setClickable(true);
        ll_container.setLayoutParams(getDefaultParams(Gravity.BOTTOM, 16, 16));

        /** title */
        mTvTitle = new TextView(mContext);
        mTvTitle.setGravity(Gravity.CENTER);
        mTvTitle.setPadding(dp2px(10), dp2px(5), dp2px(10), dp2px(5));
        mTvTitle.setSingleLine(true);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = dp2px(20);

        ll_container.addView(mTvTitle, params);

        /** title underline */
        mVLineTitle = new View(mContext);
        ll_container.addView(mVLineTitle);

        /** listview */
        mLv = new ListView(mContext);
        mLv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1));
        mLv.setCacheColorHint(Color.TRANSPARENT);
        mLv.setFadingEdgeLength(0);
        mLv.setVerticalScrollBarEnabled(false);
        mLv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mLv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        ll_container.addView(mLv);

        /** mCancel btn */
        mTvCancel = new TextView(mContext);
        mTvCancel.setGravity(Gravity.CENTER);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.topMargin = dp2px(8);
        lp.bottomMargin = dp2px(8);
        mTvCancel.setLayoutParams(lp);

        ll_container.addView(mTvCancel);

        return ll_container;
    }

    @Override
    protected void setupUiView() {
        super.setupUiView();
        /** title */
        mTvTitle.setHeight(dp2px(mTitleHeight));
        mTvTitle.setBackground(CornerUtils.cornerDrawable(mTitleBgColor, new float[]{mCornerRadius, mCornerRadius, mCornerRadius,
                mCornerRadius, 0, 0, 0, 0}));
        mTvTitle.setText(mTitle);
        mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleTextSize);
        mTvTitle.setTextColor(mTitleTextColor);
        mTvTitle.setVisibility(mTitle != null ? View.VISIBLE : View.GONE);

        /** title underline */
        mVLineTitle.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dp2px(mDividerHeight)));
        mVLineTitle.setBackgroundColor(mDividerColor);
        mVLineTitle.setVisibility(mTitle != null ? View.VISIBLE : View.GONE);

        /** mCancel btn */
        mTvCancel.setHeight(dp2px(mItemHeight));
        mTvCancel.setText(mCancelText);
        mTvCancel.setTextSize(TypedValue.COMPLEX_UNIT_SP, mCancelTextSize);
        mTvCancel.setTextColor(mCancelTextColor);
        mTvCancel.setBackground(CornerUtils.listItemSelector(mCornerRadius, mDialogBgColor, mItemPressColor, 1, 0));

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        /** listview */
        mLv.setDivider(new ColorDrawable(mDividerColor));
        mLv.setDividerHeight(dp2px(mDividerHeight));

        if (mTitle != null) {
            mLv.setBackground(CornerUtils.cornerDrawable(mDialogBgColor, new float[]{0, 0, 0, 0, mCornerRadius, mCornerRadius, mCornerRadius,
                    mCornerRadius}));
        } else {
            mLv.setBackground(CornerUtils.cornerDrawable(mDialogBgColor, mCornerRadius));
        }

        if (mAdapter == null) {
            mAdapter = new ListDialogAdapter();
        }

        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(mOnItemClickListener);

        mLv.setLayoutAnimation(mLac);
    }


}
